/**
 * @author helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.ManufacturerDao) SALE.dao.local.ManufacturerDao = function(){
	var manufacturerDoc;
}

SALE.dao.local.ManufacturerDao.prototype = {	

		/**
		 * 
		 */
		initManufacturerDoc : function() {
			if(!this.manufacturerDoc) {
				this.manufacturerDoc = LocalXMLFileUtil.getManufacturerDoc();
			}
			
			return this.manufacturerDoc;
		},
		
		saveManufacturerDoc : function() {
			LocalXMLFileUtil.saveXML(this.manufacturerDoc,LocalXMLFileUtil.getManufacturerFile());
		},
			
		insertManufacturerInfo : function(manufacturerJson) {
			var root = this.manufacturerDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			manufacturerJson["id"] = id;
			root.setAttribute("maxid",id);
			var manufacturerNode = this.manufacturerDoc.createElement("manufacturer");
			
			this.updateManufacturer(manufacturerJson, manufacturerNode);			
			root.appendChild(manufacturerNode);
			
			this.saveManufacturerDoc();
		},
		
		modifyManufacturerInfo : function(manufacturerJson) {
			var manufacturerNode = this.searchManufacturerByID(manufacturerJson.id);
			
			this.updateManufacturer(manufacturerJson, manufacturerNode);
			
			this.saveManufacturerDoc();
		},
		
		deleteManufacturerInfo : function(id) {
			var manufacturerNode = this.searchManufacturerByID(id);
			var root = this.manufacturerDoc.documentElement;
			root.removeChild(manufacturerNode);
		},
				
		updateManufacturer : function(manufacturerJosn, manufacturerNode) {
			
			for(var attri in manufacturerJosn) {	
				
				if(attri.indexOf("old") != 0 && manufacturerJosn[attri] && manufacturerJosn[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, manufacturerNode);
					if(attriNodes != null && attriNodes.length > 0) {
						manufacturerNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.manufacturerDoc.createElement(attri);
					var contextNode = this.manufacturerDoc.createCDATASection(manufacturerJosn[attri]);
					attriNode.appendChild(contextNode);
					manufacturerNode.appendChild(attriNode);
				}
			}

		},
		
		searchManufacturerByID : function(id) {
			var root = this.manufacturerDoc.documentElement;
			var objArray = Ext.query("manufacturer:has(id:nodeValue(" + id+ "))", root);
			var result;			
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchManufacturerJsonByID : function(id) {
			
			var resultNode = this.searchManufacturerByID(id);
			
			var result = {};
			
			if (resultNode != null) {
				var children = resultNode.childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			
			return result;
		},
		
		buildManufacturersJson : function() {
			var root = this.initManufacturerDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/manufacturer", root);
				var manufacturersJson = {manufacturers: []};
				
				Ext.each(objArray, function(manufacturer) {
					var children = manufacturer.childNodes;
					var manufacturerJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!manufacturerJson[child.nodeName] && child.childNodes[0] != null) {
								manufacturerJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					manufacturersJson.manufacturers.push(manufacturerJson);
				});
				
				return manufacturersJson;
			}
		}	
}